
"""遍历并打印目录中的文件"""
import os
import re
class FileChange:
    pattern=re.compile(r".java$")
    pattern2=re.compile(r"println")
    def reDetect(self,filename):
        res=self.pattern.search(filename)
        if res:
            return True
        else:
            return False
    def detectPrint(self,path):
        with open(path,"r") as f:
            str=f.read()
            res=self.pattern2.search(str)
            if res:
                print(res)
                newStr=re.sub(r"println","输出",str)
                print(newStr)
                return True
            else:
                return False
    def printDir(self,path):
        for filename in os.listdir(path):
            tmpPath = os.path.join(path,filename)
            if(os.path.isdir(tmpPath)):
                self.printDir(tmpPath)
            else:
                if self.reDetect(filename):
                    if self.detectPrint(tmpPath):
                        print("该java文件内含有println:"+tmpPath)


if __name__ == '__main__':
    path= "D:/java-test/testRe"
    fc=FileChange()
    fc.printDir(path)